﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MedReg
{
    public partial class Regime : IEquatable<Regime>
    {
        public void ImportList(List<Classes.Regime> regList)
        {
            var mdb = new MedregDBDataContext();
            var ListReg = new List<Regime>();
            foreach (var reg in regList)
            {
                Regime reg1 = new Regime() { ID = reg.ID, Name = reg.Name };
                Regime query;

                if (mdb.Regime.LongCount() == 0)
                {
                    mdb.Regime.InsertOnSubmit(reg1);
                }
                else
                {
                    try
                    {
                        query = mdb.Regime.Single(l => l.ID == reg1.ID);
                        query.Name = reg1.Name;

                    }
                    catch (ArgumentNullException e)
                    {
                    }
                    catch (InvalidOperationException e)
                    {

                        if (!ListReg.Contains(reg1))
                        {
                            mdb.Regime.InsertOnSubmit(reg1);
                            ListReg.Add(reg1);
                        }
                    }
                }
            }
            mdb.SubmitChanges();
        }
        public bool Equals(Regime other)
        {
            var i = this.ID.Equals(other.ID);
            var j = this.Name.Equals(other.Name);
            if (i & j)
            {
                return true;
            }
            return false;
        }
    }
}
